package com.example.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.voip.R;

public class CallRecordsAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<Map<String,String>> data;
	private Context context;

	public CallRecordsAdapter(Context context,List<Map<String,String>> data) {
		this.mInflater = LayoutInflater.from(context);
		this.data = data;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if(convertView == null){
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.item_callrecords, null);
			holder.name = (TextView)convertView.findViewById(R.id.call_num);
			holder.time = (TextView)convertView.findViewById(R.id.call_time);
			holder.call = (Button)convertView.findViewById(R.id.call_to);
			holder.type = (ImageView)convertView.findViewById(R.id.call_type);
			convertView.setTag(holder);
		}else{
			holder = (Holder)convertView.getTag();
		}
		
		holder.name.setText(data.get(position).get("name"));
		holder.time.setText(data.get(position).get("time"));
		try {
			holder.type.setImageDrawable(context.getResources().getDrawable(R.drawable.class.getField(data.get(position).get("type")).getInt(new R.drawable())));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return convertView;
	}
	
	class Holder{
		TextView name;
		TextView time;
		ImageView type;
		Button call;
	}


}
